;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.GuaranteesApplyDetail", [])
        .controller("GuaranteesApplyDetailCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter, $stateParams) {
            var btnPermissionCode = {
                guaranteesApplyDel: window.ActionCode.guaranteesApplyDel,
            };
            $scope.showDatedeliveryReceiptStart = false;
            $scope.showDatedeliveryReceiptEnd = false;

            $scope.btnPermission = {
                guaranteesApplyEdit: true,
            };

            $scope.condition = {};
            $scope.statusList =[
                {code:0,name:"未出库"},
                {code:1,name:"部分出库"},
                {code:2,name:"已出库"},
            ]

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: { left: $scope.tableConfig.fixedWidth + "px" }
                };
            };
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.tableConfig = {
                fixedWidth: 95,
                scrollWidth: 1400,
                tableCol: [
                    {
                        type: "",
                        name: "三包申请单号",
                        dataField: "purchaseNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单据状态",
                        dataField: "statusDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "customerName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDesc",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单位",
                        dataField: "unit",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "申请数量",
                        dataField: "purchaseQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "实际库存",
                        dataField: "amount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已建出库数量",
                        dataField: "deliveryQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "实际出库数量",
                        dataField: "realQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "产品型号",
                        dataField: "prodType",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库时间",
                        dataField: "outDate",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "服务人员",
                        dataField: "serviceName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "服务网点",
                        dataField: "serviceSite",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "CRM单号",
                        dataField: "crmOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },{
                        type: "",
                        name: "服务信息单号",
                        dataField: "serviceNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否返厂",
                        dataField: "isReturn",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "实际返厂",
                        dataField: "actualReturnF",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "status",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "序列号",
                        dataField: "factoryNumber",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "签收时间",
                        dataField: "receiveDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 55,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.editApply = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    if (!data.headerInfo.dispatcherName) {
                        data.headerInfo.dispatcherName = $scope.curUser.name;
                    }
                    if (!data.headerInfo.sapTypeName) {
                        data.headerInfo.sapTypeName = "Z01";
                    }
                    if (!data.headerInfo.settleTypeName) {
                        data.headerInfo.settleTypeName = "寄付";
                    }
                    $state.go("app.sales.guaranteesApply", { guaApplyDetailVO: { orderObj: data, operate: 2 } })
                });
            };

            $scope.dataList = [];

            $scope.markInfo = {
                allChecked: false,
                cueRow: -1
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.billStateList = [];
            
            function getDataList(curPage) {
                $scope.markInfo = {
                    allChecked: false,
                    cueRow: -1
                };
                var service = "device";
                var reqUrl = "/threeGuaranteesPur/listDetail";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
               
                param.pageNum = $scope.pageInfo.curPage;
              
                for (var key in $scope.condition) {
                    if ($scope.condition[key] === "" || $scope.condition[key] == null) {
                        $scope.condition[key] = undefined;
                    }
                    
                }
                param.filters = $scope.condition;
                param.filters.seller = true;
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.status = $filter("ProgressStatus")(item.status, 'outStatus');
                        item.realAuditDateShow = $filter("YbDate")(item.realAuditDate, 'yyyy-MM-dd HH:mm:ss');
                        item.applicationDateShow = $filter("YbDate")(item.applicationDate, 'yyyy-MM-dd HH:mm:ss');
                        item.deliveryReceiptTime = $filter("YbDate")(item.deliveryReceiptTime, 'yyyy-MM-dd HH:mm:ss');
                        item.receiveDateShow = $filter("YbDate")(item.receiveDate, 'yyyy-MM-dd HH:mm:ss');
                        item.outDate = $filter("YbDate")(item.outDate, 'yyyy-MM-dd HH:mm:ss');
                        item.actualReturnF = $filter("TrueOrFalse")(Number(item.actualReturn), 'normal');
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    return resp.value.list;
                });
            }

            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $state.go("app.sales.guaranteesApply", { guaApplyDetailVO: { orderObj: data, operate: 3 } })
                });
            }

            function getDetail(item) {
                var param = {
                    id: item.threeGuaPurNo
                };
                return ybHttp.Get('device', '/threeGuaranteesPur/item', param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }

            function getOrderStatues() {
                var service = "device";
                var reqUrl = "/dataDictionary/searchList";
                var param = { parentCode: "SPZT" };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.billStateList = resp.value;
                })
            }

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/threeGuaranteesPur/export"),
                sectionParam: { ids: [] },
                allUrl: ybHttp.RequestUrl("device", "/threeGuaranteesPur/exportAll")
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                geBillIds();
            };

            function geBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.threeGuaPurNo);
                    }
                });
            }

            $scope.$watch('condition.warehouseCode', function (newVal) {
                if (newVal) {
                    $scope.exportParam.sectionParam.warehouseCode = newVal;
                } else {
                    delete $scope.exportParam.sectionParam.warehouseCode;
                }
            });

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
                geBillIds();
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            (function () {
                var guaranteesApplyBillNo = window.sessionStorage.getItem("guaranteesApplyBillNo");
                window.sessionStorage.removeItem("guaranteesApplyBillNo");
                if (!!guaranteesApplyBillNo) {
                    $scope.condition.purchaseNo = guaranteesApplyBillNo;
                    getDataList().then(function (data) {
                        viewDetail(data[0]);
                    })
                } else {
                    getDataList();
                }
                getOrderStatues();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
        ;
})(window, window.angular);